package com.hd.appcommon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.appcommon.constant.DeptConstant;
import com.hd.appcommon.entity.Dept;
import com.hd.appcommon.entity.Node;
import com.hd.appcommon.entity.vo.DeptSelectVo;
import com.hd.appcommon.mapper.DeptMapper;
import com.hd.appcommon.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author administrator
 * @since 2020-11-24
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    @Override
    public List<DeptSelectVo> getDeptSelectVo(Long deptId) {

        List<Dept> allLowNodeDepts = this.getAllLowNodeDepts(deptId);
        List<DeptSelectVo> res = new ArrayList<>();
        // 放入第一层
        for (Dept dept : allLowNodeDepts) {
            DeptSelectVo deptSelectVo = new DeptSelectVo(dept.getName(), dept.getId());
            res.add(deptSelectVo);
        }
        return res;
    }

    public Node search(Long deptId, Node tree) {

        if (deptId.equals(tree.getId()))
            return tree;

        Node target = null;
        List<Node> nodes = tree.getNodes();

        for (Node node : nodes) {
            target = search(deptId, node);
            if (target != null)
                break;
        }
        return target;
    }


    /**
     * @param deptId 部门id
     * @param tree   部门树
     * @return 叶子节点为1 ,越上层数字越大
     */
    public Integer hierarchy(Long deptId, Node tree) {

        Node searchTree = this.search(deptId, tree);

        Integer pos = 0;

        Queue<Node> queue = new LinkedList<>();
        // 初始的部门
        queue.offer(searchTree);
        while (!queue.isEmpty()) {
            int size = queue.size();
            // 一次一层
            for (int i = 0; i < size; i++) {
                Node node = queue.poll();
                List<Node> nodes = node.getNodes();
                for (Node n : nodes) {
                    queue.offer(n);
                }
            }
            pos++;
        }

        return pos;
    }


    @Override
    public List<String> getHasAdminLeaderids() {


        return baseMapper.getHasAdminLeaderids();
    }

    public List<Dept> getAllLowNodeDepts(Long deptId) {

        Node root = search(deptId, DeptConstant.tree);

        Queue<Node> queue = new LinkedList<>();
        List<Dept> res = new LinkedList<>();
        // 初始的部门
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            // 一次一层
            for (int i = 0; i < size; i++) {
                Node node = queue.poll();
                res.add(node.getDept());

                List<Node> nodes = node.getNodes();
                for (Node n : nodes) {
                    queue.offer(n);
                }
            }
        }
        return res;
    }


    public List<Dept> getAllLowNodeDepts2(Long deptId) {
        Queue<Long> queue = new LinkedList<>();
        List<Dept> res = new LinkedList<>();
        // 初始的部门
        queue.offer(deptId);
        while (!queue.isEmpty()) {
            int size = queue.size();
            // 一次一层
            for (int i = 0; i < size; i++) {
                Long node = queue.poll();
                Dept byId = this.getById(node);
                res.add(byId);
                // 把一个node 对应的部门的所有的子部门全部放入queque 中, 查询子部门
                QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("pid", node);
                List<Dept> list = this.list(queryWrapper);
                for (Dept dept : list) {
                    queue.offer(dept.getId());
                }
            }
        }
        return res;
    }

    // 找到某个学生的学院
    public List<Dept> getFatherDepts(Long deptId) {
        Queue<Long> queue = new LinkedList<>();
        List<Dept> res = new ArrayList<>();
        // 初始的部门
        queue.offer(deptId);
        while (!queue.isEmpty()) {
            Long node = queue.poll();
            if (node == 0L) break;
            Dept byId = this.getById(node);
            res.add(byId);
            queue.offer(byId.getPid());
        }
        return res;
    }




    public String getDeptChildren(String deptId) {
        List<Dept> allLowNodeDepts = this.getAllLowNodeDepts(Long.valueOf(deptId));
        StringBuilder sb = new StringBuilder();
        sb.append("'");
        for (int i = 0; i < allLowNodeDepts.size(); i++) {
            if (i < allLowNodeDepts.size() - 1) {
                sb.append(allLowNodeDepts.get(i).getId()).append("|");
            } else {
                sb.append(allLowNodeDepts.get(i).getId());
            }
        }
        sb.append("'");

        return sb.toString();
    }

    public List<Dept> getDeptAndFatherDept(String[] deptIds) {
        return deptMapper.getDeptAndFatherDept(deptIds);
    }

    @Override
    public List<String> getIds(QueryWrapper<Dept> queryWrapper) {
        return deptMapper.getIds(queryWrapper);
    }

    @Override
    public String getName(String classes){
        List<String> names = deptMapper.getNameStrings(new QueryWrapper<Dept>().eq("id",classes));
        if(names.size()==0){
            return null;
        }
        else return names.get(0);
    }
}
